package demo.java.lang.management;

import org.junit.Test;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class MemoryManagerMXBeanDemo {

    void demoGarbageCollectorMXBean(GarbageCollectorMXBean gc) {
        // 返回垃圾收集器花费的总时间（以毫秒为单位）
        gc.getCollectionTime();
        // 返回垃圾收集器已执行的总次数
        gc.getCollectionCount();


    }

    @Test
    public void testGarbageCollectorMXBean() {
        List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gcBean : gcBeans) {
            System.out.println("Garbage Collector: " + gcBean.getName());
            System.out.println("Collection Count: " + gcBean.getCollectionCount());
            System.out.println("Collection Time (ms): " + gcBean.getCollectionTime());
            System.out.println();
        }
    }
}
